package cn.iocoder.yudao.module.signature.controller.admin.signinfo;

import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.signature.controller.app.signinfo.vo.ApplicationSignInfoPageReqVO;
import cn.iocoder.yudao.module.signature.controller.app.signinfo.vo.ApplicationSignInfoRespVO;
import cn.iocoder.yudao.module.signature.controller.app.signinfo.vo.ApplicationSignInfoSaveReqVO;
import cn.iocoder.yudao.module.signature.dal.dataobject.signinfo.ApplicationSignInfoDO;
import cn.iocoder.yudao.module.signature.dal.mysql.signinfo.ApplicationSignInfoMapper;
import cn.iocoder.yudao.module.signature.service.signinfo.ApplicationSignInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 -  应用签名记录")
@RestController
@RequestMapping("/signature/application-sign-info")
@Validated
public class ApplicationSignInfoController {

    @Resource
    private ApplicationSignInfoMapper applicationSignInfoMapper;

    @Resource
    private ApplicationSignInfoService applicationSignInfoService;

    @PostMapping("/delete")
    @Operation(summary = "删除应用签名记录")
    @PreAuthorize("@ss.hasPermission('signature:application-sign-info:delete')")
    public CommonResult<Boolean> deleteApplicationSignInfo(@RequestBody List<Long> ids) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        applicationSignInfoService.deleteApplicationSignInfo(ids,loginUser.getId());
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得应用签名记录分页")
    @PreAuthorize("@ss.hasPermission('signature:application-sign-info:query')")
    public CommonResult<PageResult<ApplicationSignInfoRespVO>> getApplicationSignInfoPage(@Valid ApplicationSignInfoPageReqVO pageReqVO) {
            Page<ApplicationSignInfoDO> page = new Page<>((long) (pageReqVO.getPageNo()-1) * pageReqVO.getPageSize(), pageReqVO.getPageSize());
        IPage<ApplicationSignInfoDO> result = applicationSignInfoMapper.list(page,pageReqVO);
        PageResult<ApplicationSignInfoDO> pageResult = new PageResult<>(result.getRecords(), result.getTotal());
        return success(BeanUtils.toBean(pageResult, ApplicationSignInfoRespVO.class));
    }
}